Skip to content

Preserve switch fallthrough around non-final default arms - #685

Merged
nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-4nmjyr868j
Nov 26, 2026
Merged

nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-4nmjyr868j

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

Valid C++ switches can silently execute the wrong branches when default is not last, in either output model. For selector 99, default_chain in tests/unit/switch_default_fallthrough.cpp should append 9 then 1, but the translation returns 9 instead of 91. The fix also prevents a final case without a redundant break from falling into an earlier default.

cc @lucic71 @nunoplopes

Keep switch-macro bodies in source order and defer the unguarded wildcard only in the generated dispatch match. Dispatch still jumps to the original body label; ordinary fallthrough then enters the next source body. Guarded wildcard arms retain their original priority.

Plain matches and default-last switches keep their existing output.

Testing

Lit passes 46 switch/goto fixtures, each in refcount and unsafe mode. Cargo's switch-related control-flow tests report 15 passed, 0 failed and 0 ignored. The added case in libcc2rs-macros/tests/control_flow.rs distinguishes a guarded wildcard from default even when a later case overlaps it.

C++ and Rust formatting checks and macro Clippy checks pass.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@nunoplopes
nunoplopes merged commit 039e051 into Cpp2Rust:master Nov 26, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants